import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/views/login'
Vue.use(VueRouter)

const router = new VueRouter({
    mode: "history",
    base: process.env.BASE_URL,
    routes:[
      {path:'/',redirect:'login'},
      {path: '/login', name: 'login', component: login},
      {path:'/layout', name:'layout',component:() => import("@/layout/Layout.vue"),children:[
        {path:'/home', name:'home',component: ()=> import("@/views/Home.vue")},
        {path:'/stuinfo', name:'stuinfo',component: ()=> import("@/views/stuInfo.vue")},
        {path:'/roominfo', name:'roominfo',component: ()=> import("@/views/roominfo.vue")},
        {path:'/visitinfo', name:'visitinfo',component: ()=> import("@/views/VisitInfo.vue")},
        {path:'/repairInfo', name:'repairinfo',component: ()=> import("@/views/repairInfo.vue")},
        {path:'/WeiguiInfo', name:'weiguiInfo',component: ()=> import("@/views/WeiguiInfo.vue")},
        {path:'/absentRoomInfo', name:'absentRoomInfo',component: ()=> import("@/views/absentRoomInfo.vue")},
        {path:'/HolidayInfo', name:'HolidayInfo',component: ()=> import("@/views/HolidayInfo.vue")},
        {path:'/myRoomInfo', name:'myroominfo',component: ()=> import("@/views/MyRoomInfo.vue")},
        {path:'/applyabsentRoom', name:'applyabsentRoom',component: ()=> import("@/views/applyabsentRoom.vue")},
        {path:'/applyhoildayInfo', name:'applyhoildayinfo',component: ()=> import("@/views/applyhoildayInfo.vue")},
        {path:'/dormManagerInfo', name:'dormManagerInfo',component: ()=> import("@/views/dormManagerInfo.vue")},
        {path:'/buildingInfo', name:"buildingInfo",component: ()=> import("@/views/buildingInfo.vue")},
        {path:'/applyRepairInfo', name:"applyRepairInfo",component: ()=> import("@/views/applyRepairInfo.vue")},
        {path:'/selfInfo', name:"selfInfo",component: ()=> import("@/views/selfInfo.vue")},
      ]}
    ]
  }
)

router.beforeEach((to, from, next) => {
  // 1.如果访问的是登录页面（无需权限），直接放行
  if (to.path === '/login') return next()
  const user= window.localStorage.getItem('user')
  if (!user) {
    alert("请登录")
    return next('/login')
  }
  next()
})

export default router